<div id="content">
    <h3><a href="../index.php?p=home">Geoplaza</a><a href="index.php?p=home"> &rsaquo;&rsaquo; Onderwijs</a> &rsaquo;&rsaquo; Mijn profiel bewerken</h3>
    <div id="Profilehead">Profiel bewerken</div>
    <div id="profile">
        <?php
        require_once('../config/appvars.php');
        require_once('../config/config.php');

        // Make sure the user is logged in before going any further.
        if (!isset($_SESSION['user_id']) && $_SESSION['pakket'] == 'a') {
            echo '<p class="login">Please <a href="../index.php?p=home">log in</a> to access this page.</p>';
            exit();
        }
        if ($_SESSION['pakket'] == 'aa') {
            // Connect to the database
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

            if (isset($_POST['submit'])) {
                // Grab the profile data from the POST
                $instellingnaam = mysqli_real_escape_string($dbc, trim($_POST['instellingnaam']));
                $adres = mysqli_real_escape_string($dbc, trim($_POST['adres']));
                $postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));
                $woonplaats = mysqli_real_escape_string($dbc, trim($_POST['woonplaats']));
                $provincie = mysqli_real_escape_string($dbc, trim($_POST['provincie']));
                $telefoon = mysqli_real_escape_string($dbc, trim($_POST['telefoon']));
                $email = mysqli_real_escape_string($dbc, trim($_POST['email']));

                if (!empty($instellingnaam) && !empty($adres) && !empty($postcode) && !empty($woonplaats) && !empty($provincie) && !empty($telefoon) && !empty($email)) {

                    $query = "UPDATE onderwijsinstelling SET instellingNaam = '$instellingnaam', 
                    adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', 
                    provincie = '$provincie', telefoon = '$telefoon', email = '$email',
                    WHERE onderwijs_id = '" . $_SESSION['user_id'] . "'";

                    
                    mysqli_query($dbc, $query);

                    $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=viewprofileaa';
                    header('Location: ' . $home_url);

                    mysqli_close($dbc);
                    exit();
                } else {
                    echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
                }
            } // End of check for form submission
            else {
                // Grab the profile data from the database
                $query = "SELECT instellingNaam, adres, postcode, woonplaats, provincie, 
                    telefoon, email FROM onderwijsinstelling WHERE onderwijs_id = " . $_SESSION['user_id'] . ";";
               
                $data = mysqli_query($dbc, $query);
                $row = mysqli_fetch_array($data);

                if ($row != NULL) {
                    $instellingnaam = $row['instellingNaam'];
                    $adres = $row['adres'];
                    $postcode = $row['postcode'];
                    $woonplaats = $row['woonplaats'];
                    $provincie = $row['provincie'];
                    $telefoon = $row['telefoon'];
                    $email = $row['email'];
                } else {
                    echo '<p class="error">There was a problem accessing your profile.</p>';
                }
            }
            mysqli_close($dbc);
        } else {
            echo '<p class="login">Please <a href="../index.php?p=home">log in</a> to access this page.</p>';
            exit();
        }
        ?>
        <form enctype="multipart/form-data" method="post" action="index.php?p=editprofileaa">
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
            <fieldset class="info">
                <legend>Werkgever informatie</legend>
                <p class="titel"> Instellingnaam</p>
                <input name="instellingnaam" id="instellingnaam" class="edit" type="text" value="<?php if (!empty($instellingnaam))
            echo $instellingnaam; ?>"/>

                <p class="titel">Adres</p>
                <input name="adres" id="adres" class="edit" type="text" value="<?php if (!empty($adres))
                           echo $adres; ?>"/>

                <p class="titel">Postcode</p>
                <input name="postcode" id="postcode" class="edit" type="text" value="<?php if (!empty($postcode))
                           echo $postcode; ?>"/>

                <p class="titel">Woonplaats</p>
                <input name="woonplaats" id="woonplaats" class="edit" type="text"value="<?php if (!empty($woonplaats))
                           echo $woonplaats; ?>" />

                <p class="titel">Provincie</p>
                <select name="provincie" class="edit">
                    <option value="Groningen" <?php if (!empty($provincie) && $provincie == 'Groningen')
                           echo 'selected = "selected"'; ?>>Groningen</option>
                    <option value="Friesland" <?php if (!empty($provincie) && $provincie == 'Friesland')
                                echo 'selected = "selected"'; ?>>Friesland</option>
                    <option value="Drenthe" <?php if (!empty($provincie) && $provincie == 'Drenthe')
                                echo 'selected = "selected"'; ?>>Drenthe</option>
                    <option value="Overijssel" <?php if (!empty($provincie) && $provincie == 'Overijssel')
                                echo 'selected = "selected"'; ?>>Overijssel</option>
                    <option value="Flevoland" <?php if (!empty($provincie) && $provincie == 'Flevoland')
                                echo 'selected = "selected"'; ?>>Flevoland</option>
                    <option value="Gelderland" <?php if (!empty($provincie) && $provincie == 'Gelderland')
                                echo 'selected = "selected"'; ?>>Gelderland</option>
                    <option value="Utrecht" <?php if (!empty($provincie) && $provincie == 'Utrecht')
                                echo 'selected = "selected"'; ?> >Utrecht</option>
                    <option value="Noord-Holland" <?php if (!empty($provincie) && $provincie == 'Noord-Holland')
                                echo 'selected = "selected"'; ?>>Noord-Holland</option>
                    <option value="Zuid-Holland" <?php if (!empty($provincie) && $provincie == 'Zuid-Holland')
                                echo 'selected = "selected"'; ?>>Zuid-Holland</option>
                    <option value="Zeeland" <?php if (!empty($provincie) && $provincie == 'Zeeland')
                                echo 'selected = "selected"'; ?>>Zeeland</option>
                    <option value="Noord-Barabant" <?php if (!empty($provincie) && $provincie == 'Noord-Barabant')
                                echo 'selected = "selected"'; ?>>Noord-Barabant</option>
                    <option value="Limburg" <?php if (!empty($provincie) && $provincie == 'Limburg')
                                echo 'selected = "selected"'; ?>>Limburg</option>
                </select>

                <p class="titel">Telefoon</p>
                <input name="telefoon" id="telefoon" class="edit" type="text" value="<?php if (!empty($telefoon))
                                echo $telefoon; ?>" />

                     <p class="titel">Gebruikersnaam (je email)</p>
                     <input name="email" id="email" type="text" class="edit" value="<?php if (!empty($email))
                                echo $email; ?>"/>
                     
            </fieldset>
            <input type="submit" value="Save Profile" name="submit" />
        </form>
    </div>
</div>